<?php

declare(strict_types=1);

namespace app\api\controller;

use app\api\service\Discount as DiscountService;
use app\facade\Basic;
use think\exception\ValidateException;
use think\Request;
use  app\api\traits\ValidateTrait;
class Discount
{
    use ValidateTrait; // 使用 Trait
    protected $request;
    protected $service;
    protected $validationScenes  = [
        'index'=>[
            'page'=>['require|integer', 'staff.pageIndex'],
            'listRows'  => [['require|integer', 'between:1,100'], 'staff.pageSize'],
        ],

    ];
    public function __construct(Request $request, DiscountService $service)
    {
        $this->request = $request;
        $this->service = $service;
    }
    /**
     * @describe 检查优惠券码
     * @author: Bobo
     * @date: 2025-05-14 13:35:37 
     * @param [type] @required
     * @return Renderable
     */
    public function checkCode()
    {

        $params = $this->request->param();
        $params['user_id'] = $this->request->user_id;
        $params['role'] = $this->request->role == 'user' ? 1 : 2;
        $res = $this->service->checkCode($params);
        return Basic::json($res);
    }


    /**
     * @describe 我的优惠券
     * @author: xu
     * @date: 2025-05-14 13:35:37
     * @param [type] @required
     * @return Renderable
     */
    public function myCoupon()
    {
        try {
            $this->autoValidate('index');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return Basic::json(['code'=>100001, 'message'=>$customErrors ]);
        }
        $params = $this->request->param();
        $params['user_id'] = $this->request->user_id;
        $params['role'] = $this->request->role == 'user' ? 1 : 2;
        $res = $this->service->myCoupon($params);
        return Basic::json($res);
    }
}
